//
//  HLBillVC.m
//  zhuanxian
//
//  Created by zhaoliang chen on 2017/11/17.
//  Copyright © 2017年 huiling. All rights reserved.
//

#import "HLBillVC.h"
#import "ZLCollectionViewFlowLayout.h"
#import "HLStationCell.h"
#import "UITextField+function.h"
#import "HLWaybillDetailVC.h"
#import "HLUnderlineView.h"
#import "HLWebsiteObject.h"

@interface HLBillVC ()
<ZLCollectionViewFlowLayoutDelegate,UICollectionViewDelegate,
UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView* collectionViewStation;
@property(nonatomic,strong)NSArray* arrayStations;
@property(nonatomic,strong)UITextField* textFieldSearch;

@end

@implementation HLBillVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setNavigationTitle:@"到港网点" Color:[UIColor whiteColor]];
    
    HLUnderlineView* view = [[HLUnderlineView alloc]init];
    view.backgroundColor = MainColor;
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self.view);
        make.height.mas_equalTo(60);
    }];
    
    [view addSubview:self.textFieldSearch];
    [self.textFieldSearch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(view).insets(UIEdgeInsetsMake(10, 8, 10, 8));
    }];
    
    [self.view addSubview:self.collectionViewStation];
    [self.collectionViewStation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(view.mas_bottom);
    }];
    
    [self getWebsites];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)onClickSearch {
    
}

- (void)searchContentChange:(UITextField*)sender {
    if (sender.text.length == 0) {
        
    }
}

- (void)getWebsites {
    [HLWebManager getWebsites:^(id response) {
        if ([response[@"code"]integerValue]==1) {
            self.arrayStations = [HLWebsiteObject mj_objectArrayWithKeyValuesArray:response[@"websiteList"]];
            [self.collectionViewStation reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrayStations.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HLStationCell* cell = [HLStationCell cellWithCollectionView:collectionView forIndexPath:indexPath];
    HLWebsiteObject* station = self.arrayStations[indexPath.item];
    cell.labelStation.text = station.websiteName;
    return cell;
}

- (ZLLayoutType)collectionView:(UICollectionView *)collectionView layout:(ZLCollectionViewFlowLayout *)collectionViewLayout typeOfLayout:(NSInteger)section {
    return ClosedLayout;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(ZLCollectionViewFlowLayout*)collectionViewLayout columnCountOfSection:(NSInteger)section {
    return 3;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    HLWebsiteObject* station = self.arrayStations[indexPath.item];
    HLWaybillDetailVC* vc = [[HLWaybillDetailVC alloc]initWithAddWaybill:station];
    [self.navigationController pushViewController:vc animated:YES];
}

- (UICollectionView*)collectionViewStation {
    if (!_collectionViewStation) {
        ZLCollectionViewFlowLayout *flowLayout = [[ZLCollectionViewFlowLayout alloc] init];
        flowLayout.delegate = self;
        
        _collectionViewStation = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _collectionViewStation.dataSource = self;
        _collectionViewStation.delegate = self;
        _collectionViewStation.backgroundColor = [UIColor whiteColor];
        [_collectionViewStation registerClass:[HLStationCell class] forCellWithReuseIdentifier:[HLStationCell cellIdentifier]];
    }
    return _collectionViewStation;
}

- (UITextField*)textFieldSearch {
    if (!_textFieldSearch) {
        _textFieldSearch = [[UITextField alloc]init];
        _textFieldSearch.placeholder = @"输入网点名称";
        UIButton* btnSearch = [UIButton buttonWithType:UIButtonTypeCustom];
        btnSearch.frame = CGRectMake(0, 0, 40, 20);
        [btnSearch setImage:[UIImage imageNamed:@"hl_home_search"] forState:UIControlStateNormal];
        [btnSearch addTarget:self action:@selector(onClickSearch) forControlEvents:UIControlEventTouchUpInside];
        btnSearch.imageView.contentMode = UIViewContentModeCenter;
        btnSearch.contentHorizontalAlignment= UIControlContentHorizontalAlignmentCenter;
        btnSearch.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
        _textFieldSearch.rightView = btnSearch;
        _textFieldSearch.rightViewMode = UITextFieldViewModeAlways;
        _textFieldSearch.layer.borderColor = [UIColor grayColor].CGColor;
        _textFieldSearch.layer.borderWidth = 0.6;
        _textFieldSearch.layer.cornerRadius = 25;
        [_textFieldSearch setLeftInset:15];
        [_textFieldSearch setLeftInset:15];
        [_textFieldSearch addTarget:self action:@selector(searchContentChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _textFieldSearch;
}

@end
